package com.xci.platform.auth.controller;

import com.xci.platform.annotation.Authorize;
import com.xci.platform.auth.entity.UserEntity;
import com.xci.platform.auth.service.UserService;
import com.xci.platform.core.BoolMessage;
import com.xci.platform.core.JsonGrid;
import com.xci.platform.helper.ExcelHelper;
import com.xci.platform.helper.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 系统用户控制器
 */
@Authorize
@Slf4j
@Controller
@RequestMapping("/auth/user")
public class UserController extends AuthBaseController {

    /**
     * 系统用户服务
     */
    @Resource
    protected UserService userService;

    //region 页面视图

    /**
     * 用户首页
     */
    @GetMapping
    @Authorize(code = "SystemUser")
    public String index() {
        return "auth/user/index";
    }

    /**
     * 用户新建页面
     */
    @GetMapping("/create")
    @Authorize(code = "SystemUser.Create")
    public String create(ModelMap map) {
        createMark(map);
        UserEntity entity = new UserEntity();
        entity.setId(StringHelper.guid());
        entity.setStatus(1);
        map.put("entity", entity);
        return "auth/user/edit";
    }

    /**
     * 用户编辑页面
     */
    @GetMapping("/edit")
    @Authorize(code = "SystemUser.Edit")
    public String edit(String id, ModelMap map) {
        UserEntity entity = userService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        return "auth/user/edit";
    }

    /**
     * 用户详细页面
     */
    @GetMapping("/details")
    @Authorize(code = "SystemUser.Details")
    public String details(String id, ModelMap map) {
        UserEntity entity = userService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        map.put("selfDataMessage", userService.queryMapDataSetting(entity.getId()));
        map.put("dataMessage", userService.queryOwnDataSetting(entity.getId()));
        return "auth/user/details";
    }

    /**
     * 修改密码页面
     */
    @GetMapping("/modify-password")
    public String modifyPassword() {
        return "auth/user/modify-password";
    }

    //endregion

    //region 数据处理

    /**
     * 组织机构成员表格数据
     */
    @ResponseBody
    @GetMapping("/user-member-details")
    public JsonGrid userMemberDetails(String departmentId, String name) {
        return BoolMessage.grid(userService.queryByDepartmentId(departmentId, name));
    }

    /**
     * 用户表格数据
     */
    @ResponseBody
    @GetMapping("/grid")
    @Authorize(code = "SystemUser.Search")
    public JsonGrid grid(@RequestParam Map params) {
        return BoolMessage.pagegrid(userService.query(params));
    }

    /**
     * 保存用户
     */
    @ResponseBody
    @PostMapping("/create-save")
    @Authorize(code = "SystemUser.Create")
    public BoolMessage createSave(@ModelAttribute UserEntity entity) {
        return userService.save(entity, true);
    }

    /**
     * 保存用户
     */
    @ResponseBody
    @PostMapping("/edit-save")
    @Authorize(code = "SystemUser.Edit")
    public BoolMessage editSave(@ModelAttribute UserEntity entity) {
        return userService.save(entity, false);
    }

    /**
     * 修改用户启用状态
     */
    @ResponseBody
    @PostMapping("/status")
    @Authorize(code = "SystemUser.Status")
    public BoolMessage status(String id, Integer status) {
        return userService.updateStatus(id, status);
    }

    @ResponseBody
    @PostMapping("/modify-password")
    public BoolMessage modifyPassword(String currentPassword, String newPassword) {
        return userService.modifyPassword(currentUser().getUserId(), currentPassword, newPassword);
    }

    @ResponseBody
    @PostMapping("/revise-password")
    @Authorize(code = "SystemUser.RevisePassword")
    public BoolMessage revisePassword(String userIds, String password) {
        return userService.revisePassword(StringHelper.splitToArray(userIds), password);
    }

    /**
     * 删除用户
     */
    @ResponseBody
    @PostMapping("/delete")
    @Authorize(code = "SystemUser.Delete")
    public BoolMessage delete(String ids) {
        return userService.delete(StringHelper.splitToArray(ids), currentUser());
    }

    /**
     * 导出用户
     */
    @GetMapping("/export")
    @Authorize(code = "SystemUser.Export")
    public void export(@RequestParam Map params, HttpServletResponse response) throws IOException {
        ExcelHelper.exportWeb(response, userService.query(params), UserEntity.class, "系统用户");
    }
    //endregion
}
